<?php

class Core_Backend_Component_Ebook_Controller_Material extends Dcmp_Component_Controller {

    public function indexAction($pageindex = 1, $pagesize = 20, $sort = 'id', $dir = 'asc', $areaid = 0) {
        $this->getHistory()->record();
        $list = App::getModel('media/collection_ebook_material')
                        ->pagination($pageindex, $pagesize);
        $list->load();
        $records = $list->loadCount();
        return $this->viewResult(array('list' => $list, 'records' => $records));
    }

    public function createAction() {
        return $this->viewResult();
    }

    public function editAction($id) {
        $model = App::getModel('media/ebook_material')->load($id);
        return $this->createAction()->setModel($model);
    }

    public function generateAction($id) {
        $model = App::getModel('media/ebook_material')->load($id);
        $poiCount = $model->getPois()->loadCount();
        $pageCount = ceil($poiCount / $model->getPoiPageSize());
        Kd_Util_IO::makeDirectory(DP_TMP . 'ebook' . DS . 'material' . DS . $id);
        return $this->templateResult(array('model' => $model, 'page_count' => $pageCount));
    }

    public function generatePictureAction($id, $page) {
        $path = DP_TMP . 'ebook' . DS . 'material' . DS . $id . DS . $page . '.jpg';
        $cmd = BP . 'IECapt.exe --url=http://192.168.1.108:88/ebook/material/poilist/id/' . $id . '/page/' . $page
                . ' --delay=500 --out=' . $path;
        exec($cmd);
        return $this->contentResult('');
    }

    public function compressAction($id) {
        $model = App::getModel('media/ebook_material')->load($id);
        Kd_Util_IO::makeDirectory(DP_TMP . 'download' . DS . 'ebook' . DS . 'material');
        $files = Kd_Util_IO::getFiles(DP_TMP . 'ebook' . DS . 'material' . DS . $id);
        $zipFile = DP_TMP . 'download' . DS . 'ebook' . DS . 'material' . DS . $id . '.zip';
        $zip = new ZipArchive();
        $res = $zip->open($zipFile, ZIPARCHIVE::OVERWRITE);
        foreach ($files as $file) {
            $zip->addFile($file['filename'], $file['name'] . '.jpg');
        }
        $zip->close();

        header("Content-type:application/octet-stream");
        header("Accept-Ranges:bytes");
        header("Accept-Length:" . filesize($zipFile));
        header("Content-Disposition:attachment;filename=" . $model->getTitle().'.zip');

        $fp = fopen($zipFile, "r");
        $content = fread($fp, filesize($zipFile));
        echo $content;
        die();
    }

    public function savePostAction() {
        $data = $this->getRequest()->getForm('material');
        $model = App::getModel('media/ebook_material');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data);
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            $this->getMessager()->addInform($this->__('share.message.success'));
            return $this->redirectResult($this->getHistoryUrl('ebook/material/index'));
        } else {
            $this->getMessager()->addError($this->__('share.message.fail'))
                    ->addError($errors);
            return $this->createAction()->setModel($model);
        }
    }
}